#!/bin/sh
#
# Plugin to monitor PHE sensors.
#
# Usage: Place in /etc/munin/node.d/ (or link it there  using ln -s)
#
# Parameters understood:
#
# 	config   (required)
# 	autoconf (optional - used by munin-config)
#
# Magic markers - optional - used by installation scripts and
# munin-config:
#
#%# family=auto
#%# capabilities=autoconf

#read owfs config, then export it so backticks work
. /etc/default/owfs
export CLIENT_OPTS

if [ "$1" = "autoconf" ]; then
	if owdir $CLIENT_OPTS ; then
		echo yes
		exit 0
	else
		echo no
		exit 1
	fi
fi


if [ "$1" = "config" ]; then

	MAX="126"
	MIN="-2"
#	if [ "$scaleto100" = "yes" ]; then
#		graphlimit=100
#	else
#		graphlimit=$PERCENT
#	fi
#	WARNING="80"
#	CRITICAL="95"
	echo 'graph_title PHE Temperatures'
	echo "graph_order solin solout tankin tankout pump"
#	echo "graph_args --base 1000 -r --lower-limit 0 --upper-limit $graphlimit"
	echo 'graph_vlabel Degrees Celcius'
#	echo 'graph_scale no'
	echo 'graph_info This graph shows plate heat exchanger temperatures.'
	echo 'graph_category solar'
	echo 'graph_period minute'
	echo 'solin.label solar-in'
	echo 'solin.draw LINE2'
	echo "solin.max $MAX"
	echo "solin.min $MIN"
	echo 'solin.type GAUGE'
	echo "solin.info Temp solar flow into PHE"
	echo 'solout.label solar-out'
	echo 'solout.draw LINE1'
	echo "solout.min $MIN"
	echo "solout.max $MAX"
	echo 'solout.type GAUGE'
	echo 'solout.info Temp solar return'
	echo 'tankin.label thermosiphon-in'
	echo 'tankin.draw LINE1'
	echo "tankin.min $MIN"
	echo "tankin.max $MAX"
	echo 'tankin.type GAUGE'
	echo 'tankin.info Temp tank into PHE'
	echo 'tankout.label thermosiphon-out'
	echo 'tankout.draw LINE2'
	echo "tankout.min $MIN"
	echo "tankout.max $MAX"
	echo 'tankout.type GAUGE'
	echo 'tankout.info Temp Thermosiphon out'
	echo 'pump.label pump-running'
	echo 'pump.draw LINE'
#	echo 'pump.color black'
#	echo "pump.min 0"
#	echo "pump.max 1"
	echo 'pump.type GAUGE'
	echo 'pump.info Solar pump runnning'

#if [ "$scaleto100" = "yes" ]; then
#	echo "system.cdef system,$NCPU,/"
#	echo "user.cdef user,$NCPU,/"
#	echo "nice.cdef nice,$NCPU,/"
#	echo "idle.cdef idle,$NCPU,/"
#fi
	exit 0
fi


SENSOR1="10.38158A010800"
SENSOR2="10.B33D8A010800"
SENSOR3="10.BB4D8A010800"
SENSOR4="10.C2E889010800"

SOLIN=`owread $CLIENT_OPTS $SENSOR1/temperature`
SOLOUT1=`owread $CLIENT_OPTS $SENSOR2/temperature`
SOLOUT=`echo $SOLOUT1-3 | bc`
TANKIN=`owread $CLIENT_OPTS $SENSOR3/temperature`
TANKOUT=`owread $CLIENT_OPTS $SENSOR4/temperature`
PUMP=`cat /sys/bus/i2c/devices/0-0038/read`
if [ $PUMP = 255 ]; then PUMP=0; else PUMP=5; fi

	echo "solin.value $SOLIN"
	echo "solout.value $SOLOUT"
	echo "tankin.value $TANKIN"
	echo "tankout.value $TANKOUT"
	echo "pump.value $PUMP"

